from datareport.api import ImageUtil
from datareport.impl import StaticDF
from datareport.impl.paragraph.base.BaseParagraph import BaseParagraph
from datareport.api.annotation.Style import style


@style()
class ShiJi_KaiZhanChangCi_ShiJianFenbu_19(BaseParagraph):
    """
    实际-时间分布
    """

    def __init__(self):
        super().__init__()
        self.text = ('''(1) 从时间分布来看:{}最高，为{}场次，平均{}场次/支部，分析来看，\
主要与{}有关。其次是{}为{}场次,平均{}场次/支部，主要与{}有关''')
        self.sort = 10
        self.section = 2

    def plot(self, df, plt):
        pass

    def getData(self, result):
        pass

    def start(self, conn, year, plt):
        df = StaticDF.getDF()
        df['month']=df['month'].values+'月'
        count_df=df.groupby('month').sum().sort_values('num', ascending=False)
        max_month_prs=df[df['month']==count_df.index.values[0]]['partyBranchName'].drop_duplicates()
        min_month_prs=df[df['month']==count_df.index.values[len(count_df)-1]]['partyBranchName'].drop_duplicates()
        max_top_three=df[df['month']==count_df.index.values[0]]['type'].value_counts().nlargest(3)
        min_top_three=df[df['month']==count_df.index.values[len(count_df)-1]]['type'].value_counts().nlargest(3)
        arr1='、'.join(max_top_three.index.tolist())
        arr2='、'.join(min_top_three.index.tolist())
        self.text=self.text.format(count_df.index.values[0],
                                   count_df.iloc[0].at['num'],
                                   # 平均场次/支部
                                   round(count_df.iloc[0].at['num']/len(max_month_prs),1),
                                   arr1,
                                   count_df.index.values[len(count_df)-1],
                                   count_df.iloc[len(count_df)-1]['num'],
                                   # 最小平均
                                   round(count_df.iloc[len(count_df)-1].at['num'] / len(min_month_prs), 1),
                                   arr2
                                   )
        df['场次']=1
        table=df[['month','type','场次']].groupby(['month','type']).sum().reset_index(drop=False)
        table.rename(columns={'month':'月份','type':'主题'},inplace=True)
        self.tables.append(table)
        self.image=ImageUtil.plot(count_df[['num']].reset_index(drop=False),'实际开展月度情况','月份','场次',rotation=0)

if __name__ == '__main__':
    t=ShiJi_KaiZhanChangCi_ShiJianFenbu_19()
